package com.example.demo.controller;

import com.example.demo.common.AESEncryptor;
import com.example.demo.common.ApiSign;
import com.example.demo.common.Result;
import com.example.demo.common.constant.RuleCode;
import com.example.demo.entity.config.RsaKey;
import com.example.demo.entity.dto.CompanyDTO;
import com.example.demo.verify.ParamValid;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/password")
@Api(tags = "15、加解密管理")
@ApiSupport(author = "罗海林", order = 15)
@Validated
@RequiredArgsConstructor
public class PasswordController {
    private final RsaKey rsaKey;
    private final ApiSign apiSign;

    @PostMapping("/getRsaKey")
    @ApiOperation(value = "获取RSA密钥", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 1)
    public Result getRsaKey() throws Exception {
        return rsaKey.getKey();
    }

    @PostMapping("/encrypt")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldData", value = "原数据")
    })
    @ApiOperation(value = "加密数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    public Result encrypt(@RequestParam String oldData) throws Exception {
        String newData = rsaKey.encrypt(oldData);

        Result res = new Result();
        res.ok();
        res.put("newData", newData);
        return res;
    }

    @PostMapping("/decrypt")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newData", value = "加密的数据")
    })
    @ApiOperation(value = "解密数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 3)
    public Result decrypt(@RequestParam String newData) throws Exception {
        String oldData = rsaKey.decrypt(newData);

        Result res = new Result();
        res.ok();
        res.put("oldData", oldData);
        return res;
    }

    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @ApiOperation(value = "登录", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 4)
    public Result login(@ParamValid(name = "用户名", rule = RuleCode.mobile) @RequestParam String username,
                        @ParamValid(name = "密码", rule = RuleCode.noEmpty) @RequestParam String password) {
        Result res = new Result();
        res.ok();
        res.put("token", "lhlysyl:" + username + "@" + password);
        return res;
    }

    @PostMapping("/http")
    @ApiOperation(value = "访问", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 5, includeParameters = { "name" })
    public Result http(@RequestBody @Validated({ CompanyDTO.Delete.class }) CompanyDTO companyDTO) {
        Result res = new Result();
        res.ok();
        res.put("data", companyDTO);
        return res;
    }

    @PostMapping("/AES")
    @ApiOperation(value = "对称加密", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 6)
    public Result AES(@RequestParam String content) throws Exception {
        String encryptedText = AESEncryptor.encrypt(content);
        String decryptedText = AESEncryptor.decrypt(encryptedText);

        Result res = new Result();
        res.ok();
        res.put("加密后", encryptedText);
        res.put("解密后", decryptedText);
        return res;
    }

    @PostMapping("/getData")
    @ApiOperation(value = "获取AES加密数据", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 7)
    public Result getData(@ParamValid(name = "加密数据", rule = RuleCode.noWick) @RequestParam String data) {
        Result res = new Result();
        res.ok();
        res.put("data", data);
        return res;
    }

    @PostMapping("/getPassword")
    @ApiOperation(value = "获取各种密码", response = Result.class)
    @ApiOperationSupport(author = "罗海林", order = 8)
    public Result getPassword(@ParamValid(name = "原始数据", rule = RuleCode.noWick) @RequestParam String data) {
        String result1 = apiSign.hashData(data, "MD5");
        String result2 = apiSign.hashData(data, "SHA-256");
        String result3 = apiSign.hashData(data, "SHA-512");

        Result res = new Result();
        res.ok();
        res.put("MD5", result1);
        res.put("SHA-256", result2);
        res.put("SHA-512", result3);
        return res;
    }
}
